package org.metasketch.editor.timeline;

import java.util.ArrayList;
import java.util.List;

import org.metasketch.editor.Editor;
import org.metasketch.editor.Project;

public class SnapshotEngine {

	List<Snapshot> history = new ArrayList<Snapshot>();
	int index = -1;

	// public int getLength() {
	// return history.size();
	// }

	public boolean canUndo() {
		return index > 1;
	}

	public boolean canRedo() {
		return history.size() > 0 && index < history.size();
	}

	public void takeSnapshot(Project project) {

		if (index >= 0 && index < history.size() - 1)
			history.subList(index + 1, history.size()).clear();

		Snapshot snapshot = SnapshotHandler.getSnapshot(project);

		history.add(snapshot);

		++index;
	}

	public void undo(Editor editor) {
		if (index <= 0)
			return;

		index--;

		// if (index < 0)
		// index = 0;

		Snapshot snapshot = null;

		if (index > 0)
			snapshot = history.get(index - 1);
		else
			snapshot = history.get(0);

		Project project = SnapshotHandler.getProject(snapshot);

		project.compileIndexes();

		editor.setProject(project);
		// TODO set the perspective and diagram and tool....parameters,
		// attributes, whatever

	}

	public void redo(Editor editor) {

		if (index >= history.size())

			return;

		index++;

		Snapshot snapshot = null;

		if (index > 0)
			snapshot = history.get(index-1);
		else
			snapshot = history.get(0);

		Project project = SnapshotHandler.getProject(snapshot);

		project.compileIndexes();

		editor.setProject(project);
		// TODO set the perspective and diagram and tool....parameters,
		// attributes, whatever
	}

	public void clear() {
		history.clear();

	}

}
